import type { DirectiveBinding } from 'vue';

// 这会在 `mounted` 和 `updated` 时都调用
const hasPerms = (el: any, binding: DirectiveBinding<string[]>) => {
  const { value } = binding;
  if (!value || !value.length) throw new Error('v-has-perms 指令的值不能为空');
  if (!el.parentNode) throw new Error('v-has-perms 不能用于根节点！');

  const { permissions, roles } = useUserStore();

  const hasAdmin = hasAdminRole(roles);
  const hasPerms = permissions.some((permission) => value.includes(permission));

  // 非管理员，且没有权限
  if (!hasAdmin && !hasPerms) {
    el.parentNode.removeChild(el);
  }
};

export default hasPerms;
